import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

const state = {
  token: '',
  superAdmin: '',
  active: '',
  museumId: '',
  museumName: '',
  museums: [],
  cancelTokenArr: [],
  userName:'',
  menus:[],
  // isv:''
}

const mutations = {
  storageToStore(state, data) {
    let token = localStorage.getItem('token')
    let superAdmin = localStorage.getItem('superAdmin')
    let museumId = localStorage.getItem('museumId')
    let museumName = localStorage.getItem('museumName')
    let museums = localStorage.getItem('museums')
    let isv = localStorage.getItem('isv')
    let userName=localStorage.getItem('userName')
    if(isv){
      state.isv =isv
      console.log(isv)
    }
    if (token != undefined) {
      state.token = token
      console.log(token)
    }
    if (userName != undefined) {
      state.userName = userName
      console.log(token)
    }
    if (superAdmin != undefined) {
      state.superAdmin = superAdmin
    }
    if (museumId != undefined) {
      state.museumId = museumId
    }
    if (museums != undefined) {
      state.museums = JSON.parse(localStorage.getItem('museums'))
    }
    if (museumName != undefined) {
      state.museumName = museumName
    }
  },
  signOut(state) {
    localStorage.clear()
    state.token = ''
    state.superAdmin = ''
    state.museumId = ''
    state.userName=''
  },
  setMenus(state, data) {
    state.menus = data
  },
  routerToStore(state, data) {
    state.active = data.name
  },
  pushToken (state, payload) {
    state.cancelTokenArr.push(payload.cancelToken)
  },
  clearToken (state) {
    state.cancelTokenArr.forEach(item => {
      item('cancel')
    })
    state.cancelTokenArr = []
  },

}

const getters = {

}

const actions = {

}

const modules = {

}

export default new Vuex.Store({
  state,
  getters,
  actions,
  mutations,
  modules
})